import pandas as pd
import numpy as np

# read titanic_train.csv
titanic_survival = pd.read_csv('titanic_train.csv')

# print fist 5 rows
print(titanic_survival.head())

# get page column
# NaN stands for "not a number", to indicate a missing data
age = titanic_survival['Age']
print(age)

# we can use the pandas.isnull() function which takes a pandas series
# and returns a series of True and False values
age_is_null = pd.isnull(age)
print(age_is_null)

# print age_is_null
age_null_true = age[age_is_null]
print(age_null_true)

# print the number of age_null_count
age_null_count = len(age_null_true)
print(age_null_count)

# The result of this is that mean_age would be NaN.
# This is because any calculations we do with a null value also result in a null value
mean_age = sum(titanic_survival['Age']) / len(titanic_survival['Age'])
print(mean_age)

# we have to filter
good_ages = titanic_survival['Age'][age_is_null == False]
correct_mean_age = sum(good_ages) / len(good_ages)
print(correct_mean_age)

# missing data is so common that many pandas methods automatically filter for it
correct_mean_age = titanic_survival['Age'].mean()
print(correct_mean_age)

# mean fare for each class
passenger_classes = [1, 2, 3]
fares_by_classes = {}
for this_class in passenger_classes:
    pclass_rows = titanic_survival[titanic_survival['Pclass'] == this_class]
    pclass_fares = pclass_rows['Fare']
    fare_for_class = pclass_fares.mean()
    fares_by_classes[this_class] = fare_for_class
print('---')
print(fares_by_classes)

# index tells the method which column to group by
# values is the column that we want to apply the calculation to
# aggfunc specifies the calculation we want to perform
# default aggfunc is np.mean
passenger_survival = titanic_survival.pivot_table(index='Pclass', values='Survived', aggfunc=np.sum)
print(passenger_survival)

passenger_age = titanic_survival.pivot_table(index='Pclass', values='Age')
print(passenger_age)

port_stats = titanic_survival.pivot_table(index='Embarked', values=['Fare', 'Survived'],
                                          aggfunc=np.sum)
print(port_stats)

# specifying axis=1 or axis='columns' will drop any columns that have null values
drop_na_columns = titanic_survival.dropna(axis=1)
print(drop_na_columns)

# drop rows that have null values
new_titanic_survival = titanic_survival.dropna(axis=0, subset=['Age', 'Sex'])
print(new_titanic_survival)


row_index_83_age = titanic_survival.loc[83, 'Age']
print(row_index_83_age)

row_index_1000_pclass = titanic_survival.loc[766, 'Pclass']
print(row_index_1000_pclass)

new_titanic_survival = titanic_survival.sort_values("Age", ascending=False)
print(new_titanic_survival)

titanic_reindexed = new_titanic_survival.reset_index(drop=True)
print(titanic_reindexed.loc[0:10])
